from pydantic import BaseModel, Field
from typing import Optional, List
from datetime import datetime
from enum import Enum

class JobStatus(str, Enum):
    QUEUED = "queued"
    STARTING = "starting"
    RUNNING = "running"
    COMPLETED = "completed"
    FAILED = "failed"
    STOPPED = "stopped"

class JobType(str, Enum):
    SPIDER_RUN = "spider_run"
    DEPENDENCY_INSTALLATION = "dependency_installation"

class JobBase(BaseModel):
    spider_id: str = Field(..., description="关联的爬虫ID")

class JobCreate(JobBase):
    job_type: JobType = Field(JobType.SPIDER_RUN, description="任务类型")

class JobUpdate(BaseModel):
    status: Optional[JobStatus] = Field(None, description="任务状态")
    started_at: Optional[datetime] = Field(None, description="开始时间")
    completed_at: Optional[datetime] = Field(None, description="完成时间")
    exit_code: Optional[int] = Field(None, description="退出代码")
    error_message: Optional[str] = Field(None, description="错误信息")
    container_id: Optional[str] = Field(None, description="容器ID")
    log_path: Optional[str] = Field(None, description="日志路径")

class JobRead(JobBase):
    id: str = Field(..., description="任务唯一标识符")
    status: JobStatus = Field(..., description="任务状态")
    job_type: JobType = Field(..., description="任务类型")
    created_at: datetime = Field(..., description="创建时间")
    started_at: Optional[datetime] = Field(None, description="开始时间")
    completed_at: Optional[datetime] = Field(None, description="完成时间")
    exit_code: Optional[int] = Field(None, description="退出代码")
    error_message: Optional[str] = Field(None, description="错误信息")
    
    class Config:
        orm_mode = True